package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Garrett"}, name="LostAndFound", version=1.1D)
public class LostAndFound extends Random
{
  final int appendN = 8995;
  final int appendE = 8994;
  final int appendS = 8997;
  final int appendW = 8996;

  final int[] answerN = { 32, 64, 135236, 67778, 135332, 34017, 202982, 101443, 101603, 236743, 33793, 67682, 135172, 236743, 169093, 33889, 202982, 67714, 101539 };
  final int[] answerE = { 4, 6, 101474, 101473, 169124, 169123, 67648, 135301, 135298, 67651, 169121, 33827, 67652, 236774, 101479, 33824, 202951 };
  final int[] answerS = { 4228, 32768, 68707, 167011, 38053, 230433, 164897, 131072, 168068, 65536, 35939, 103589, 235718, 204007, 100418, 133186, 99361, 136357, 1057, 232547 };
  final int[] answerW = { 105571, 37921, 131204, 235751, 1024, 165029, 168101, 68674, 203974, 2048, 100451, 6144, 39969, 69698, 32801, 136324 };

  final int setting = 531;

  public boolean activateCondition() {
    if (this.game.isLoggedIn());
    return this.objects.getNearest(new int[] { 8995 }) != null;
  }

  public RSObject getFarthestObjectByID(int[] paramArrayOfInt) {
    Object localObject = null;
    double d1 = -1.0D;
    for (int i = 0; i < 104; i++) {
      for (int j = 0; j < 104; j++) {
        RSObject[] arrayOfRSObject = this.objects.getAllAt(new RSTile(i + this.game.getBaseX(), j + this.game.getBaseY()));
        if (arrayOfRSObject.length > 0) {
          RSObject localRSObject = arrayOfRSObject[0];
          int k = 0;
          for (int i1 : paramArrayOfInt) {
            if (localRSObject.getID() == i1) {
              k = 1;
              break;
            }
          }
          if (k != 0) {
            double d2 = this.calc.distanceBetween(getMyPlayer().getLocation(), localRSObject.getLocation());
            if (localObject == null) {
              d1 = d2;
              localObject = localRSObject;
            } else if (d2 > d1) {
              localObject = localRSObject;
              d1 = d2;
            }
          }
        }
      }
    }
    return localObject;
  }

  private int getOddAppendage() {
    int[] arrayOfInt1 = this.settings.getSettingArray();
    try
    {
      int k;
      for (k : this.answerN) {
        if (arrayOfInt1[531] == k)
          return 8995;
      }
      for (k : this.answerE) {
        if (arrayOfInt1[531] == k)
          return 8994;
      }
      for (k : this.answerS) {
        if (arrayOfInt1[531] == k)
          return 8997;
      }
      for (k : this.answerW)
        if (arrayOfInt1[531] == k)
          return 8996;
    }
    catch (Exception localException) {
    }
    return random(8994, 8998);
  }

  public int loop() {
    if (this.interfaces.canContinue()) {
      this.interfaces.clickContinue();
    }
    if (this.objects.getNearest(new int[] { 8995 }) == null) {
      return -1;
    }
    int i = getOddAppendage();
    try
    {
      RSObject localRSObject = getFarthestObjectByID(new int[] { i });
      RSTile localRSTile = localRSObject.getLocation();
      if (!this.calc.tileOnScreen(localRSTile)) {
        this.walking.getPath(localRSTile).traverse();
        sleep(random(700, 900));
        while (getMyPlayer().isMoving()) {
          sleep(100);
        }
      }
      if (localRSObject.doAction("Operate")) {
        sleep(random(1000, 1500));
        while (getMyPlayer().isMoving())
          sleep(100);
      }
    }
    catch (Exception localException)
    {
    }
    return random(50, 100);
  }
}